package com.limao.AsyncListener;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.Hazelcast.ManagerCache;
import com.limao.DbConnection.lankServer;
import com.limao.Tool.CacheClass;
import com.unionpay.acp.sdk.SDKConfig;



@WebListener
public class AppContextListener implements ServletContextListener {
	
	ManagerCache hCache ;
	
	@Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
         // create the thread pool
    	System.out.println("初始化开始");
    	
    	
    	
    	ServletContext sc = servletContextEvent.getServletContext();
    	String lan = (String) sc.getInitParameter("LAN");
    	
    	if(lan.equalsIgnoreCase("2"))
    	{
    		CacheClass.isOut=2;
    	}
    	else if(lan.equalsIgnoreCase("1"))
    	{
    		CacheClass.isOut=1;
    	}
    	else
    		CacheClass.isOut=0;
    	
    	SDKConfig.getConfig().loadPropertiesFromSrc();
    	
    	CacheClass.imgUrl2  = (String) sc.getInitParameter("imgBaseurl2");
    	CacheClass.imgUrl1  = (String) sc.getInitParameter("imgBaseurl1");
    	CacheClass.imgUrl0  = (String) sc.getInitParameter("imgBaseurl0");
    	
    	CacheClass.webUrl2  = (String) sc.getInitParameter("domain2");
    	CacheClass.webUrl1  = (String) sc.getInitParameter("domain1");
    	CacheClass.webUrl0  = (String) sc.getInitParameter("domain0");
    	
    	CacheClass.mw2  = (String) sc.getInitParameter("mw2");
    	CacheClass.mw1  = (String) sc.getInitParameter("mw1");
    	CacheClass.mw0  = (String) sc.getInitParameter("mw0");
    	
    	CacheClass.tradeulr2  = (String) sc.getInitParameter("tradeulr2");
    	CacheClass.tradeulr1  = (String) sc.getInitParameter("tradeulr1");
    	CacheClass.tradeulr0  = (String) sc.getInitParameter("tradeulr0");
    	
    	CacheClass.task  = CacheClass.getIntValue((String) sc.getInitParameter("task"));
    	
    	
    	
    	String iscrypt=(String) sc.getInitParameter("iscrypt");
    	CacheClass.iscrypt=CacheClass.getIntValue(iscrypt);
    	
        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 400, 50000L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(300));
        servletContextEvent.getServletContext().setAttribute("executor", executor);
        
        
        hCache=new ManagerCache();
        hCache.init();
        
        lankServer.getInstance().start();

    }
 
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

        lankServer.getInstance().stop();
        hCache.close();


        ThreadPoolExecutor executor = (ThreadPoolExecutor) servletContextEvent
                .getServletContext().getAttribute("executor");
        executor.shutdown();
  
        System.out.println("关闭");
    }

    
 

    
}
